import type { App } from 'vue'
import { setupGlobCom } from './components'
import { CORE_DEPENDENCIES, isDependencyInstalled, isElementPlusRegistered } from './utils/dependency-check'

// 只导入自定义样式，不包含Element Plus样式
import "./styles/index.scss"

// 导出所有组件，支持按需引入
export * from './components'

// Export hooks
export * from './hooks/web/useCache'
export * from './hooks/web/useConfigGlobal'
export * from './hooks/web/useCrudSchemas'
export * from './hooks/web/useDesign'
export * from './hooks/web/useEmitt'
export * from './hooks/web/useForm'
export * from './hooks/web/useGuide'
export * from './hooks/web/useI18n'
export * from './hooks/web/useIcon'
export * from './hooks/web/useLocale'
export * from './hooks/web/useMessage'
export * from './hooks/web/useNetwork'
export * from './hooks/web/useNow'
export * from './hooks/web/useNProgress'
export * from './hooks/web/usePageLoading'
export * from './hooks/web/useTable'
export * from './hooks/web/useTimeAgo'
export * from './hooks/web/useTitle'
export * from './hooks/web/useValidator'
export * from './hooks/web/useWatermark'
export * from './hooks/event/useScrollTo'

// 导出组件类型定义
// 注释掉有问题的导出
// export * from './types/components'
export * from './components/eAdvancedFilter'
export * from './components/eCustomTable'
export * from './components/eSearchForm'
export * from './components/Form'
export * from './components/Table'
export * from './components/Cropper'
export * from './components/ImageViewer'

// Export utilities
export * from './utils'
export * from './utils/dict'
export * from './utils/element-plus'

// Export types
export type * from './types/global'
export type * from './types/descriptions'
export type * from './types/form'
export type * from './types/router'

// Export dependency check utilities
// export { verifyDependencies } from './utils/dependency-check'

//注册组件
export { componentMapRegister,componentMapGet,componentMapHas,componentMapDel,componentMap } from './components/componentMap'

export { eConfirm } from './components/eConfirmDialog'
// Export dictionary utilities
export { setDictData, getDictOptions, getDictLabel, getDictObj, DICT_TYPE } from './utils/dict'
export type { DictDataType, NumberDictDataType } from './utils/dict'

// Export locale constants
export const SUPPORTED_LOCALES: LocaleType[] = ['zh-CN', 'en']

/**
 * Check if all required dependencies are installed
 * @returns true if all core dependencies are available
 */
const checkCoreDependencies = (): boolean => {
  let allInstalled = true
  
  CORE_DEPENDENCIES.forEach(dep => {
    if (!isDependencyInstalled(dep)) {
      console.error(`[EpmsElement] Required dependency "${dep}" is missing. Please install it.`)
      allInstalled = false
    }
  })
  
  return allInstalled
}

/**
 * Check for Element Plus registration conflicts
 * @param app Vue application instance
 */
const handleElementPlusConflicts = (app: App<Element>): void => {
  // Skip if Element Plus is not already registered
  if (!isElementPlusRegistered()) {
    return
  }
  
  // If Element Plus is already registered, log a warning
  console.warn(
    '[EpmsElement] Element Plus is already registered in the application. ' +
    'This may cause conflicts if you register it again. ' +
    'If you encounter duplicate component registration errors, consider using ' +
    'the standalone version of EPMS Element without Element Plus.'
  )
}

// 组件列表，用于用户手动注册
import * as components from './components'

// 安装函数，用于全量引入
const install = (app: App<Element>, options: { skipDependencyCheck?: boolean } = {}) => {
  // Check dependencies unless explicitly skipped
  if (!options.skipDependencyCheck) {
    // Verify core dependencies
    const dependenciesOk = checkCoreDependencies()
    if (!dependenciesOk) {
      console.warn('[EpmsElement] Some required dependencies are missing, functionality may be limited')
    }
  }
  
  // Check for Element Plus conflicts
  handleElementPlusConflicts(app)
  
  // 注册组件
  setupGlobCom(app)
}

// 默认导出，支持app.use(EpmsElement)全量引入
export default {
  install,
  // 导出组件对象，支持手动注册
  ...components
}
